﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using _PLATFORM.DATA;

namespace _PLATFORM.WEB.Controllers
{ 
    public class SystemsController : Controller
    {
        private eCommPlatformRepoEntities db = new eCommPlatformRepoEntities();
        private Guid UserID = _PLATFORM.DATA.Custom_Classes.User.UserID;

        //
        // GET: /Systems/

        public ViewResult Index()
        {
            var systems = db.SYSTEMs.Where(S => S.USER_ID == UserID);
            return View(systems.ToList());
        }

        //
        // GET: /Systems/Details/5

        public ViewResult Details(decimal id)
        {
            SYSTEM system = db.SYSTEMs.Find(id);
            return View(system);
        }

        //
        // GET: /Systems/Create

        public ActionResult Create()
        {
            //ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME");
            return View();
        } 

        //
        // POST: /Systems/Create

        [HttpPost]
        public ActionResult Create(SYSTEM system, IEnumerable<HttpPostedFileBase> fileBase)
        {
            if (ModelState.IsValid)
            {
                var uploads = new Dictionary<string, HttpPostedFileBase>();
                var ctr = 0;
                foreach (var files in Request.Files)
                {
                    HttpPostedFileBase file = Request.Files[ctr];
                    if (file.ContentLength > 0)
                    {
                        if (file.ContentType.Contains("jp") | file.ContentType.Contains("png") | file.ContentType.Contains("gif"))
                        {
                            system.IMAGE = UserID.ToString().Substring(0,7) + "_" + file.FileName;
                            uploads.Add("Image", file);
                        }

                        if (file.ContentType.Contains("pdf"))
                        {
                            system.PDF = UserID.ToString().Substring(0,7) + "_" + file.FileName;
                            uploads.Add("Pdf", file);
                        }
                    }
                    ctr++;
                }

                system.USER_ID = UserID;

                db.SYSTEMs.Add(system);
                db.SaveChanges();

                // Only Write Image & PDF If DB Insert Is OK
                var imagePath = Server.MapPath("~\\Assets\\SystemImages\\" + system.IMAGE);
                uploads["Image"].SaveAs(imagePath);

                imagePath = Server.MapPath("~\\Assets\\SystemPdfs\\" + system.PDF);
                uploads["Pdf"].SaveAs(imagePath);

                return RedirectToAction("Index");  
            }

            return View(system);
        }
        
        //
        // GET: /Systems/Edit/5
 
        public ActionResult Edit(decimal id)
        {
            SYSTEM system = db.SYSTEMs.Find(id);
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", system.USER_ID);
            return View(system);
        }

        //
        // POST: /Systems/Edit/5

        [HttpPost]
        public ActionResult Edit(SYSTEM system)
        {
            if (ModelState.IsValid)
            {
                db.Entry(system).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.USER_ID = new SelectList(db.FRAMEWORK_USER, "USER_ID", "FIRST_NAME", system.USER_ID);
            return View(system);
        }

        //
        // GET: /Systems/Delete/5
 
        public ActionResult Delete(decimal id)
        {
            SYSTEM system = db.SYSTEMs.Find(id);
            return View(system);
        }

        //
        // POST: /Systems/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(decimal id)
        {            
            SYSTEM system = db.SYSTEMs.Find(id);
            db.SYSTEMs.Remove(system);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}